package com.timebank.timecoin.controller;

import com.timebank.common.core.result.Result;
import com.timebank.common.core.web.controller.BaseController;
import com.timebank.common.core.web.pojo.AjaxResult;
import com.timebank.timecoin.pojo.entity.User;
import com.timebank.timecoin.service.ContractService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.gateway.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
 * 智能合约控制层
 * @author shiliuyinzhen
 */
@RestController
@RequestMapping("/contract")
@Slf4j
@AllArgsConstructor
@Api(tags = "智能合约相关接口")
public class ContractController extends BaseController {

    @Resource
    private ContractService contractService;

    /**
     * 查询所有用户信息
     * @return
     * @throws ContractException
     */
    @GetMapping("/list")
    @ApiOperation("查询所有用户信息")
    public AjaxResult queryAll() throws ContractException {
        return AjaxResult.success(contractService.queryAll());
    }

    /**
     * 根据id查询用户信息
     * @param userId
     * @return
     * @throws ContractException
     */
    @GetMapping("/{userId}")
    @ApiOperation("根据id查询用户信息")
    public AjaxResult getUser(@PathVariable("userId") Long userId) throws ContractException {
        return AjaxResult.success(contractService.getUserById(userId));
    }

    /**
     * 添加用户
     * @param user
     * @return
     * @throws ContractException
     * @throws InterruptedException
     * @throws TimeoutException
     */
    @PostMapping
    @ApiOperation("添加用户")
    public AjaxResult createUser(@RequestBody User user) throws ContractException, InterruptedException, TimeoutException {
        return toAjax(contractService.createUser(user));
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     * @throws ContractException
     * @throws InterruptedException
     * @throws TimeoutException
     */
    @PutMapping
    @ApiOperation("更新用户信息")
    public AjaxResult updateUser(@RequestBody User user) throws ContractException, InterruptedException, TimeoutException {
        return toAjax(contractService.updateUser(user));
    }
}
